<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="consumeAddForm" labelWidth="80px">
				<uni-forms-item label="栏舍编号" name="phouseCode" required>
					<uni-data-select v-model="consumeAddForm.phouseCode" placeholder="请选择栏舍编码"
						:localdata="phouseCodeList" @change="changePhouseTypeByCode"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="栏舍类型" name="phouseType" required>
					<uni-data-select v-model="consumeAddForm.phouseType" placeholder="请选择栏舍类型"
						:localdata="pType" disabled></uni-data-select>
				</uni-forms-item>

				<uni-forms-item label="存储地点" name="materiaFeedStorePosition" required>
					<uni-data-select v-model="consumeAddForm.materiaFeedStorePosition" placeholder="请选择存储地点"
						:localdata="store_position" @change="changeFeedTypyByPosion"></uni-data-select>
				</uni-forms-item>
				
				<uni-forms-item label="饲料类型" name="materiaFeedType" required>
					<uni-data-select v-if="feedTypeList.length == 0" v-model="consumeAddForm.materiaFeedType" placeholder="请选择饲料类型"
						:localdata="material_type"></uni-data-select>
					<uni-data-select v-else v-model="consumeAddForm.materiaFeedType" placeholder="请选择饲料类型"
						:localdata="feedTypeList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="数量(KG)" name="materiaFeedConsumeNum" required>
					<uni-easyinput type="number" v-model="consumeAddForm.materiaFeedConsumeNum"
						placeholder="请输入数量(KG)" />
				</uni-forms-item>
				<uni-forms-item label="负责人" name="materiaFeedManager" required>
					<uni-data-select v-model="consumeAddForm.materiaFeedManager" placeholder="请选择负责人"
						:localdata="staffNameList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="消耗时间" name="materiaFeedConsumeTime" required>
					<uni-datetime-picker type="datetime" v-model="consumeAddForm.materiaFeedConsumeTime" />
				</uni-forms-item>
				<uni-forms-item label="备注">
					<uni-easyinput type="textarea" v-model="consumeAddForm.remark" placeholder="请输入备注" />
				</uni-forms-item>
			</uni-forms>
			<button type="primary" plain="true" @click="submit('form')">提交</button>
		</view>
	</view>
</template>

<script>
	import {
		addConsume,
		delConsume,
		findAllStaffName,
		findPhouseCode,
		findStoreFeedTypeByPosition,
		findStoreNum,
		getConsume,
		listConsume,
		updateConsume, getTypes
	} from "@/api/hmy/material";

	export default {
		name: "Consume",
		//dicts: ['hmy_pig_breed_type'],
		data() {
			return {
				//饲料类型
				typesList:[],
				material_type: [
				],
				store_position: [{
						text: '一号仓库',
						value: 1
					},
					{
						text: '二号仓库',
						value: 2
					},
					{
						text: '三号仓库',
						value: 3
					}
				],
				park_type: [{
						text: '公猪舍',
						value: 1
					},
					{
						text: '母猪舍',
						value: 2
					},
					{
						text: '肉猪舍',
						value: 3
					}
				],
				consumeAddForm: {},
				rules: {
					phouseCode: {
						rules: [{
							required: true,
							errorMessage: '栏舍编码不能为空'
						}]
					},
					materiaFeedConsumeNum: {
						rules: [
							{
								required: true,
								errorMessage: '消耗数量不能为空'
							},
							{
								validateFunction: async (rule, value, data, callback) => {
									console.log('value', value)
									var positon = this.consumeAddForm.materiaFeedStorePosition;
									var type = this.consumeAddForm.materiaFeedType;
									if (positon != undefined && type != undefined) {
										await findStoreNum(type, positon).then(response => {
											this.nowStoreNum = response.data.materiaFeedStoreNum;
										})
									}
									if (this.nowStoreNum < value) {
										callback("不可超过现有库存（" + this.nowStoreNum + "）")
									}
								}
							}
						]
					},
					materiaFeedStorePosition: {
						rules: [{
							required: true,
							errorMessage: '存储地点不能为空'
						}]
					},
					materiaFeedType: {
						rules: [{
							required: true,
							errorMessage: '饲料类型不能为空'
						}]
					},
					materiaFeedManager: {
						rules: [{
							required: true,
							errorMessage: '负责人不能为空'
						}]
					},
					materiaFeedConsumeTime: {
						rules: [{
							required: true,
							errorMessage: '消耗时间不能为空'
						}]
					}
				},
				//所有的栏舍编码
				phouseCodeList: [],
				//所有物料负责人
				staffNameList: [],
				//栏舍类型
				pType: [],
				//仓库下的饲料类型
				feedTypeList:[],
				//现有存储数量
				nowStoreNum: ''
			}
		},
		created() {
			this.getTypesList();
			findPhouseCode().then(response => {
				var phouseList = response.data;
				phouseList.forEach(item => {
					this.phouseCodeList.push({
						'text': item.phouseCode,
						'value': item.phouseCode
					})
				})
				//console.log(this.phouseCodeList)
			});

			findAllStaffName().then(response => {
				var staffList = response.data;
				staffList.forEach(item => {
					this.staffNameList.push({
						'text': item.staffName,
						'value': item.staffId
					})
				})
				//console.log(this.staffNameList);
			})
		},
		onReady() {
			// 需要在onReady中设置规则
			this.$refs.form.setRules(this.rules)
		},

		methods: {
			//动态获取饲料数据
			getTypesList(){
			  getTypes().then(res => {
				this.typesList = res.data;
				this.typesList.forEach((item,index) => {
					this.material_type.push({
						'text': item.materiaFeedType,
						'value': item.materiaFeedTypeId
					})
				})
			  })
			},
			/**
			 * 根据栏舍编码改变栏舍类型
			 * @param value 当前下拉框的值
			 */
			changePhouseTypeByCode(value) {
				findPhouseCode().then(response => {
					var pList = response.data;
					pList.forEach((item,index) => {
						if (value === item.phouseCode) {
							if (item.phouseType == 1) {
								this.pType.push({
									'text': '公猪舍',
									'value': item.phouseType
								})
								this.consumeAddForm.phouseType = '1';
							} else if (item.phouseType == 2) {
								this.pType.push({
									'text': '母猪舍',
									'value': item.phouseType
								})
								this.consumeAddForm.phouseType = '2';
							} else if (item.phouseType == 3) {
								this.pType.push({
									'text': '肉猪舍',
									'value': item.phouseType
								})
								this.consumeAddForm.phouseType = '3';
							}
						}
					})
				});
			},
			// 根据仓库地址获取该仓库下的饲料类型
			changeFeedTypyByPosion(position) {
			      this.feedTypeList = [];
			      findStoreFeedTypeByPosition(position).then(response => {
			        var storeFeedTypeList = response.data;
			        storeFeedTypeList.forEach(item => {
			         this.typesList.forEach(type => {
						 if (item.materiaFeedType == type.materiaFeedTypeId) {
						   this.feedTypeList.push({'text': type.materiaFeedType, 'value': type.materiaFeedTypeId})
						   return;
						 }
					   })
			        })
			      })
				  console.log(this.feedTypeList);
			    },
			submit(form) {
				this.$refs[form].validate().then(res => {
					addConsume(this.consumeAddForm).then(response => {
						this.$modal.msgSuccess("新增成功")
						setTimeout(() => {
							this.$tab.navigateTo('/pages/hmy/material/consume');
						}, 1000)

					})
				})
			},
		}
	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	page {
		display: flex;
		flex-direction: column;
		box-sizing: border-box;
		min-height: 100%;
		height: auto;
	}

	view {
		font-size: 16px;
		line-height: inherit;
	}

	/* #endif */
	.code {
		margin-bottom: 5px;
	}

	.icon {
		float: right;
		margin-right: 10px;
		margin-top: 5px;
	}

	.message {
		margin-top: 5px;
		color: #989897;
		line-height: 25px;
	}


	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>